/**
 * 
 */
package org.rollinitiative.d20.combat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rollinitiative.d20.AdjustableValue;
import org.rollinitiative.d20.Adjustment;
import org.rollinitiative.d20.entity.AbilityListenerValue;
import org.rollinitiative.d20.entity.AbilityValue;
import org.rollinitiative.d20.entity.Size;

/**
 * This class manages attack modifiers.
 * 
 * @author bebopjmm
 *
 */
public class Attacks
{
    static final Log LOG_ = LogFactory.getLog(Attacks.class);
    
    int maxBAB_;
    
    int nAttacks_;
    
    int currentAttack_;
    
    Adjustment sizeAdj_;
    
    Adjustment grappleSize_;

    AbilityListenerValue melee_;
    
    AbilityListenerValue ranged_;
    
    AbilityListenerValue grapple_;
    
    /**
     * @param size
     * @param meleeAbility
     * @param rangedAbility
     * @param grappleAbility
     */
    public Attacks(Size size, AbilityValue meleeAbility, AbilityValue rangedAbility, AbilityValue grappleAbility)
    {
        maxBAB_ = 0;
        nAttacks_ = 0;
        sizeAdj_ = Size.generateCombatAdjustment(size);
        melee_ = new AbilityListenerValue(0, meleeAbility);
        melee_.addAdjustment(sizeAdj_);
        ranged_ = new AbilityListenerValue(0, rangedAbility);
        ranged_.addAdjustment(sizeAdj_);
        grapple_ = new AbilityListenerValue(0, grappleAbility);
        grappleSize_ = Size.generateGrappleAdjustment(size);
        grapple_.addAdjustment(grappleSize_);
    }
    
    /**
     * @param maxBaseAttackBonus
     */
    public void setBaseAttackBonus(int maxBaseAttackBonus)
    {
        LOG_.info("Setting maxBAB to: " + maxBaseAttackBonus);
        this.maxBAB_ = maxBaseAttackBonus;
        melee_.setBase(maxBAB_);
        ranged_.setBase(maxBAB_);
        grapple_.setBase(maxBAB_);
        nAttacks_ = 1 + (maxBAB_ / 5);
        LOG_.debug("Number of attacks =" + nAttacks_);
    }
    
    /**
     * @param newSize
     */
    public void setSize(Size newSize)
    {
        sizeAdj_.setValue(newSize.getCombatModifier());
        grappleSize_.setValue(newSize.getGrappleModifier());
    }
    
    /**
     * @return
     */
    public AdjustableValue getMeleeModifier()
    {
        return melee_;
    }
    
    /**
     * @return
     */
    public AdjustableValue getRangedModifer()
    {
        return ranged_;
    }
    
    /**
     * @return
     */
    public AdjustableValue getGrapple()
    {
        return grapple_;
    }
    
    /**
     * @return
     */
    public int getMaxBaseAttackBonus()
    {
        return this.maxBAB_;
    }
    
    /**
     * 
     */
    public void consume()
    {
        int newBAB = melee_.getBase() - 5;
        melee_.setBase(newBAB);
        ranged_.setBase(newBAB);
        grapple_.setBase(newBAB);
    }
    
    /**
     * 
     */
    public void reset()
    {
        melee_.setBase(maxBAB_);
        ranged_.setBase(maxBAB_);
        grapple_.setBase(maxBAB_);
    }
}
